package com.hippo.core.ui.compose.control

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.core.ext.orZero
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.ConfirmAlertDialog
import com.hippo.core.ui.components.SecondaryButton
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.navigation.AppRoute
import com.hippo.core.ui.theme.ColorBackgroundPrimary
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.ui.theme.Shapes
import com.hippo.core.vm.ConsumablesVM
import com.thingclips.bouncycastle.asn1.x500.style.RFC4519Style.title

/**
 * 耗材管理
 */
@Composable
fun ConsumablesScreen(onBack: () -> Unit,vm: ConsumablesVM = viewModel()) {

    val uiState = vm.uiState


    var clickType by remember{ mutableStateOf(0) }

    var firstDialog by remember{ mutableStateOf(false) }
    var secondDialog by remember{ mutableStateOf(false) }

    var sensorExpiredDialog by remember{ mutableStateOf(false) }
    var sideBrushExpiredDialog by remember{ mutableStateOf(false) }

    LaunchedEffect(Unit) {
        if(uiState.info?.sensorIsNotice == 1){
            sensorExpiredDialog = true
            vm.publishDps("110",uiState.info.sensorValidDay.orZero())
        }
        if(uiState.info?.sideBrushIsNotice == 1){
            sideBrushExpiredDialog = true
            vm.publishDps("109",uiState.info.sideBrushValidDay.orZero())
        }
    }



    if(firstDialog){
        AlertDialog(
            shape = Shapes.medium,
            onDismissRequest = {
                firstDialog = false
            },
            title = {
                Text(text = stringResource(id = R.string.consumables_1))
            },
            dismissButton = {
                SecondaryButton(
                    text = stringResource(id = R.string.cancel),
                    onClick = {
                        firstDialog = false
                    }
                )
            },
            confirmButton = {
                ThemeButton(
                    text = stringResource(id = R.string.sure),
                    onClick = {
                        firstDialog = false
                        secondDialog = true
                    }
                )
            }
        )
    }
    if(secondDialog){
        AlertDialog(
            shape = Shapes.medium,
            onDismissRequest = {
                secondDialog = false
            },
            title = {
                Text(text = stringResource(id = R.string.consumables_2))
            },
            dismissButton = {
                SecondaryButton(
                    text = stringResource(id = R.string.cancel),
                    onClick = {
                        secondDialog = false
                    }
                )
            },
            confirmButton = {
                ThemeButton(
                    text = stringResource(id = R.string.sure),
                    onClick = {
                        secondDialog = false

                        if(clickType == 1){
                            vm.refreshSensor()
                        }else{
                            vm.refreshBrush()
                        }

                    }
                )
            }
        )
    }



    CommonBody(
        title = stringResource(id = R.string.setting_28), onBack = onBack
    ) {

        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier
                .fillMaxSize()
                .background(ColorBackgroundPrimary)
                .verticalScroll(rememberScrollState())
        ) {

            Spacer(modifier = Modifier.height(10.dp))

            if(sensorExpiredDialog) {
                Text(
                    text = stringResource(id = R.string.consumables_hint1), style = TextStyle(
                        fontSize = 16.sp,
                        color = Color.Gray,
                    ), modifier = Modifier
                        .padding(16.dp)
                        .background(
                            color = Color.White, shape = RoundedCornerShape(16.dp)
                        )
                        .padding(20.dp)
                )
            }
            if(sideBrushExpiredDialog) {
                Text(
                    text = stringResource(id = R.string.consumables_hint2), style = TextStyle(
                        fontSize = 16.sp,
                        color = Color.Gray,
                    ), modifier = Modifier
                        .padding(16.dp)
                        .background(
                            color = Color.White, shape = RoundedCornerShape(16.dp)
                        )
                        .padding(20.dp)
                )
            }

            Spacer(modifier = Modifier.weight(1f))

            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .fillMaxWidth(0.7F)
                    .background(
                        color = Color.White, shape = RoundedCornerShape(16.dp)
                    )
            ) {


                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(
                            color = MaterialTheme.colorScheme.primary,
                            shape = RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)
                        )
                        .padding(10.dp)

                ) {
                    Image(
                        painter = painterResource(R.mipmap.white_refresh),
                        contentDescription = null,
                        modifier = Modifier
                            .height(20.dp)
                            .align(Alignment.CenterEnd)
                            .click{
                                clickType = 1
                                firstDialog = true
                            }
                    )
                }

                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(26.dp)
                ) {
                    Text(
                        text = stringResource(id = R.string.setting_29), style = TextStyle(
                            fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold
                        )
                    )

                    Spacer(modifier = Modifier.height(10.dp))

                    val sensorValidDay = uiState.info?.sensorValidDay

                    val sensor = if(sensorValidDay == null) {
                        "--"
                    } else if(sensorValidDay == 0){
                        stringResource(R.string.consumables_3)
                    }else{
                        sensorValidDay.toString()
                    }

                    Text(
                        text = sensor,
                        style = TextStyle(
                            fontSize = 30.sp,
                            color = Color.Black,
                        )
                    )
                }

            }

            Spacer(modifier = Modifier.weight(1f))

            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .fillMaxWidth(0.7F)
                    .background(
                        color = Color.White, shape = RoundedCornerShape(16.dp)
                    )
            ) {

                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(
                            color = MaterialTheme.colorScheme.primary,
                            shape = RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)
                        )
                        .padding(10.dp)

                ) {
                    Image(
                        painter = painterResource(R.mipmap.white_refresh),
                        contentDescription = null,
                        modifier = Modifier
                            .height(20.dp)
                            .align(Alignment.CenterEnd)
                            .click{
                                clickType = 2
                                firstDialog = true
                            }
                    )
                }

                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(26.dp)
                ) {

                    Text(
                        text = stringResource(id = R.string.setting_30), style = TextStyle(
                            fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold
                        )
                    )

                    Spacer(modifier = Modifier.height(10.dp))

                    val sideBrushValidDay = uiState.info?.sideBrushValidDay

                    val side = if(sideBrushValidDay == null) {
                        "--"
                    } else if(sideBrushValidDay == 0){
                        stringResource(R.string.consumables_3)
                    }else{
                        sideBrushValidDay.toString()
                    }

                    Text(
                        text = side,
                        style = TextStyle(
                            fontSize = 30.sp,
                            color = Color.Black,
                        )
                    )
                }
            }

            Spacer(modifier = Modifier.weight(3f))
        }


    }
}


@Preview(showBackground = true)
@Composable
private fun ConsumablesScreenPreview() {
    HippoTheme {
        ConsumablesScreen(onBack = {})
    }
}